'undefined reference to vtable' when adding Q 您所在的位置:网站首页 qt undefined reference to vtable 'undefined reference to vtable' when adding Q

'undefined reference to vtable' when adding Q

2023-11-14 18:23| 来源: 网络整理| 查看: 265

I'm having a compiler issue I can't understand. I have two classes which inherit from a common base class which in turn inherits from QObject. I'm doing this because I'm storing the objects in a list and want to be able to distinguish them later:

for (int i: _elements.keys()) { DrawableElement* de = _elements[i]; if (qobject_cast(de)) { DrawableElementContour* ctr = qobject_cast(de); ... } else if (qobject_cast(de)) { DrawableElementStroke* ctr = qobject_cast(de); ... }

However, when I add the Q_OBJECT macro to my DrawableElementStroke class, it causes an error when I compile:

C:/Qt/5.12.3/mingw73_32/include/QtCore/qobject.h:504: undefined reference to `Gf::DrawableElementStroke::staticMetaObject' debug/DrawableElementStroke.o: In function `ZN2Gf21DrawableElementStrokeC2EiP7QObject': D:\dev\starling2\game\2019\build-GoldfinchQt-Desktop_Qt_5_12_3_MinGW_32_bit2-Debug/../GoldfinchQt/core/DrawableElementStroke.cpp:9: undefined reference to `vtable for Gf::DrawableElementStroke' debug/DrawableElementStroke.o: In function `ZN2Gf21DrawableElementStrokeD2Ev': D:\dev\starling2\game\2019\build-GoldfinchQt-Desktop_Qt_5_12_3_MinGW_32_bit2-Debug/../GoldfinchQt/core/DrawableElementStroke.cpp:13: undefined reference to `vtable for Gf::DrawableElementStroke' collect2.exe: error: ld returned 1 exit status

This is strange because my DrawableElementContour class has almost the same structure. If I omit the Q_OBJECT from DrawableElementStroke I can compile, but then I am unable to use the qobject_cast.

These are my headers:

DrawableElement.h

class ScenePanel; class DrawableElement : public QObject { Q_OBJECT int _id; public: DrawableElement(int id, QObject* parent = nullptr); int id() { return _id; } virtual QRectF bounds() = 0; virtual void paint(QPainter&) {} virtual void toXml(QDomElement root) {} virtual void initFromXml(QDomElement root) {} };

DrawableElementStroke.h

class DrawableElementStroke : public DrawableElement { Q_OBJECT StrokeShape _stroke; public: DrawableElementStroke(int id, QObject* parent = nullptr); ~DrawableElementStroke() override; StrokeShape stroke() { return _stroke; } void setStroke(const StrokeShape& stroke) { _stroke = stroke; } QRectF bounds() override { return _stroke.bounds(); } void paint(QPainter& p) override; void toXml(QDomElement root) override; void initFromXml(QDomElement root) override; };

DrawableElementCountour.h

class DrawableElementContour : public DrawableElement { Q_OBJECT Shape2D _shape; public: DrawableElementContour(int id, QObject* parent = nullptr); ~DrawableElementContour() override; Shape2D shape() { return _shape; } void setShape(const Shape2D& shape) { _shape = shape; } QRectF bounds() override { return _shape.bounds(); } void paint(QPainter& p) override; void toXml(QDomElement root) override; void initFromXml(QDomElement root) override; };


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有